class TankMap extends eui.Component {
	/**关卡 */
	public level: number = 0;
	/**敌方坦克数量 */
	public tankCount: number = 30;
	public tankRedNum: number = 10;


	/**敌方坦克出生表 */
	private tankStack: TankInfoList[] = [];
	private curTank: TankAi[] = [];
	private myTank: Tank;
	private player1Ctrl = { w: 0, d: 1, s: 2, a: 3, j: -1 };
	private keyList = {};
	private keyIdle = true;
	static ins: TankMap;

	//--------------eui
	private lableText: eui.Label;

	public constructor() {
		super()
		this.skinName = "resource/tankSkins/TankMapSkin.exml";
	}
	/**游戏失败 */
	gameover() {
		this.showLable(true, "GameOver")
	}
	/**光卡结束 */
	gameWin() {
		this.showLable(true, "GameWin")
	}

	showLable(b: boolean, text: string) {
		if (b) this.lableText.text = text;
		this.lableText.visible = b;
	}
	startGame(level: number) {
		this.buildMap(level);
		this.tankStack = this.genTankList();
	}
	/**补充敌方坦克 */
	genTank() {
		for (var i = 0; i < 5; i++) {
			let tank = this.curTank[i];
			if ((!tank || tank.isDead) && this.tankStack.length) {
				let tankInfo = this.tankStack.pop();
				let tank = Pool.getItem(TankAi);
				tank.type = tankInfo.type;
				tank.isRed = tankInfo.red;
				let randomIndex = 1 + Math.random() * 20 | 0;
				let {x, y} = MapElement.index2pos(randomIndex);
				tank.x = x, tank.y = y;
				this.addChild(tank);
				this.curTank[i] = tank;
				return;
			}
		}
		if (this.gameCheck()) this.gameWin();
	}
	/**获取当前在战场中的坦克 */
	getTank() {
		return [...this.curTank, this.myTank];
	}

	/**检查剩余坦克数量是否为0 */
	gameCheck() {
		if (!this.tankStack.length) {
			for (var tank of this.curTank) {
				if (!tank.isDead) {
					return false;
				}
			}
			return true;
		}
		return false;
	}

	buildMap(level: number) {
		let map = MapData[level];
		for (var i = 0; i < map.length; i++) {
			this.generate(i, map[i])
		}
	}
	/**生成一份敌方坦克列表在队列 */
	genTankList() {
		let tankList = [];
		for (var i = 0; i < this.tankCount; i++) {
			tankList.push({ type: i >= (this.tankCount / 4 | 0) ? (i >= (this.tankCount / 2 | 0) ? 4 : 3) : 2, red: false })
		}
		tankList.sort(() => Math.random() - 0.5);
		for (var i = 0; i < this.tankRedNum; i++) {
			tankList[i].red = true;
		}
		tankList.sort(() => Math.random() - 0.5);
		return tankList;
	}
	/**对应格子生成障碍物 */
	generate(index: number, type: MapElementType) {
		let ele = MapElement.getElement(index);
		if (type > 0) {
			ele = ele || Pool.getItem(MapElement);
			ele.type = type;
			ele.index = index;
			this.addChild(ele);
		} else {
			ele && ele.clear();
		}
	}

	onKeydown(this: TankMap, e: KeyboardEvent) {
		if (this.keyList[e.key]) return;
		let dir = this.player1Ctrl[e.key];
		if (dir != undefined) {
			this.keyList[e.key] = 1;
			this.keyIdle = false;
		}
	}
	onKeyup(this: TankMap, e: KeyboardEvent) {
		let dir = this.player1Ctrl[e.key];
		if (dir != undefined) {
			delete this.keyList[e.key];
			if (!Object.keys(this.keyList).length) {
				this.keyIdle = true;
			}
		}
	}

	timer2Func() {
		if (this.keyIdle) return;
		if (this.myTank.isDead) return;
		if (this.keyList["j"]) this.myTank.shoot();
		for (var key in this.player1Ctrl) {
			if (this.keyList[key]) {
				let dir = this.player1Ctrl[key];
				if (dir >= 0) {
					this.myTank.move(dir);
				}
				return;
			}
		}

	}
	private keydownEvent = this.onKeydown.bind(this);
	private keyupEvent = this.onKeyup.bind(this);
	private timer = new egret.Timer(2000, 0);
	private timer2 = new egret.Timer(100, 0);
	$onAddToStage(stage: egret.Stage, nestLevel: number) {
		document.addEventListener("keydown", this.keydownEvent)
		document.addEventListener("keyup", this.keyupEvent)
		this.timer.addEventListener(egret.TimerEvent.TIMER, this.genTank, this);
		this.timer2.addEventListener(egret.TimerEvent.TIMER, this.timer2Func, this);
		this.timer.start();
		this.timer2.start();
		TankMap.ins = this;
		let data = this.myTank.getTankData();
		this.myTank.maxLife = data.life;
		this.myTank.lifeCount = 0;
		this.lableText.visible = false;
		this.startGame(this.level);
		super.$onAddToStage(stage, nestLevel);
	}
	$onRemoveFromStage() {
		MapElement.clear();
		document.removeEventListener("keydown", this.keydownEvent)
		document.removeEventListener("keyup", this.keyupEvent)
		this.timer.stop();
		this.timer2.stop();
		this.timer.removeEventListener(egret.TimerEvent.TIMER, this.genTank, this);
		TankMap.ins = null;
		super.$onRemoveFromStage();
	}

}

type TankInfoList = {
	/**类型 */
	type: number,
	/**是否为红色可掉宝 */
	red: boolean
}

//宽 40*21 高 30*21
let MapData = [
	[
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
		0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 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